package com.epam.qanda.db.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.epam.qanda.db.ConnectionManager;
import com.epam.qanda.db.dao.BaseDao;
import com.epam.qanda.db.mapping.Converter;
import com.epam.qanda.db.model.Category;

public class CategoryDao implements BaseDao {

	private final String ADD_NEW_CATEGORY = "INSERT INTO category (name) VALUES(?)";
	private final String GET_CATEGORY_BY_ID = "SELECT * FROM category WHERE id=?";
	private final String DELETE_CATEGORY_BY_ID = "DELETE FROM category WHERE id=?";
	private final String UPDATE_CATEGORY = "UPDATE category SET name=? WHERE id=?";
	private final String GET_ALL_CATEGORIES = "SELECT * FROM category";
	private static final Logger LOG = Logger.getLogger("stdout");

	@Override
	public <T> void create(T element) {
		Category category = (Category) element;
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement preparedStatement = connection.prepareStatement(ADD_NEW_CATEGORY)) {
			preparedStatement.setString(1, category.getName());
			preparedStatement.executeUpdate();
			LOG.info("Category created");
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
			throw new RuntimeException(e);
		} finally {
			ConnectionManager.getInstance().release(connection);
		}

	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T read(Integer id) {
		Connection connection = ConnectionManager.getInstance().getConnection();
		Category category = null;
		try (PreparedStatement preparedStatement = connection.prepareStatement(GET_CATEGORY_BY_ID)) {
			preparedStatement.setInt(1, id);
			ResultSet rs = preparedStatement.executeQuery();
			if (rs.next()) {
				category = Converter.<Category> fromResaultSetToObject(rs, Category.class);
			}
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
		return (T) category;
	}

	@Override
	public void delete(Integer id) {

		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement preparedStatement = connection
				.prepareStatement(DELETE_CATEGORY_BY_ID)) {
			preparedStatement.setInt(1, id);
			preparedStatement.executeUpdate();
			LOG.info("Category deleted");
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}

	}

	@Override
	public <T> void update(T element) {
		Category category = (Category) element;
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement preparedStatement = connection.prepareStatement(UPDATE_CATEGORY)) {
			preparedStatement.setString(1, category.getName());
			preparedStatement.setInt(2, category.getId());
			preparedStatement.executeUpdate();
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}

	}

	@SuppressWarnings("unchecked")
	public <T> T getAllCategories() {
		Connection connection = ConnectionManager.getInstance().getConnection();
		List<Category> categories = new ArrayList<Category>();
		try (PreparedStatement statement = connection.prepareStatement(GET_ALL_CATEGORIES)) {
			ResultSet rs = statement.executeQuery();
			while (rs.next()) {
				categories.add(Converter.<Category> fromResaultSetToObject(rs, Category.class));
			}
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
		return (T) categories;
	}

}
